<?php

class spa_paging {

    public $tableName;
    public $sqlMid;
    public $pageName;
    public $qryStrings;
    public $customSql;
    public $pageSize;
    public $language;
    public $curPage;
    protected $startRecNum = 0;
    public $pageLinks = "";
    public $linkNames;
    protected $nextLinks;
    protected $prevLinks;
    public $prevImg;
    public $nextImg;
    public $linkImg;
    public $useImages;
    public $pageVar = "spPage";
    public $className = "SPA_PgS1";

    public function __construct() {
        $this->pageSize = 10;
        $this->curPage = 1;
        $this->language = "en";
        $this->linkNames = array
            (
            "en" => array("Previous Page", "Back", "Next Page", "Next"),
        );
        $this->prevImg = "images/prev.png";
        $this->nextImg = "images/next.png";
        $this->linkImg = "images/dot.png";

        $this->useImages = false;
    }

    public function setPrevImage($imageName) {
        if ($imageName != "") {
            $this->prevImg = $imageName;
        }
    }

    public function setNextImage($imageName) {
        if ($imageName != "") {
            $this->nextImg = $imageName;
        }
    }

    public function pagingSql($sql="") {
        $this->customSql = $sql;
        if (isset($_GET[$this->pageVar])) {
            $this->curPage = htmlspecialchars($_GET[$this->pageVar]);
        }
        $pageStart = 0;
        $pageSettings = "";
        $currentPage = ($this->curPage == 1) ? 0 : ($this->curPage - 1);
        $pageStart = $currentPage * $this->pageSize;
        $pageSettings = " limit " . $pageStart . "," . $this->pageSize;
        if ($sql == "") {
            $sql = "SELECT * FROM " . $this->tableName;
        }
        return $sql . " " . $pageSettings;
    }

    public function getInfo() {
        $prevLinks = "";
        $this->pageLinks = "";
        $nextLinks = "";
        if (!isset($this->pageName)) {
            $this->pageName = "";
        }

        $totalRecs = 0;

        if ($this->customSql == "") {
            $pagingSQL = "SELECT COUNT(*) AS totalRecs FROM " . $this->tableName . $this->sqlMid;
        } else {
            $pagingSQL = $this->customSql;
        }

        if (isset($_GET[$this->pageVar])) {
            $this->curPage = htmlspecialchars($_GET[$this->pageVar]);
        }
        $startRecNum = $this->curPage * $this->pageSize;

        $resObj = mysql_query($pagingSQL); //Send a MySQL query

        if (!$resObj) {
            return "";
        }
        $resRow = mysql_fetch_assoc($resObj); //Get number of rows in result

        if ($this->customSql == "") {
            if (mysql_num_rows($resObj) > 0) {
                $totalRecs = $resRow['totalRecs']; //Get number of rows in result
            }
        } else {
            $totalRecs = mysql_num_rows($resObj);
        }

        $totalPages = ceil($totalRecs / $this->pageSize);

        $this->pageName .= "?";
        if ($this->qryStrings != "") {
            $this->pageName .= $this->qryStrings . "&";
        } else {
            $this->qryStrings = $this->getQueryStrings();
            $this->pageName .= $this->qryStrings . "&";
        }

        /*
          Limit the total pages to 15
         */
        //if($totalPages > 15) {
        //$totalPages = 15;
        //}

        for ($page = 1; $page <= $totalPages; $page++) {

            /* If current page has any query strings, add them */
            $pageInputs = $this->pageName . $this->pageVar . "=" . $page;

            /* add alt text to links */
            $alt = "Page " . $page;
            $tmpPageLinks = "<li><a href=\"";
            $tmpPageLinks .= $pageInputs;
            $tmpPageLinks .= "\" title=\"" . $alt . "\">";
            $tmpPageLinks .= $page;
            $tmpPageLinks .= "</a></li>";

            /* Current page should not have links - but in bold red */
            if ($page == $this->curPage) {
                $tmpPageLinks = "<li class=\"selected\"><a href=\"$this->pageName\" >";
                $tmpPageLinks .= $page;
                $tmpPageLinks .= "</a></li>";
            }

            /* current page is not at all printed when using images */
            if ($this->useImages && $page == $this->curPage) {
                $tmpPageLinks = "";
            }

            $this->pageLinks .= $tmpPageLinks;
        }/* end of for loop */

        /* add prev and next links to page */
        $prevLinks = "";
        $nextLinks = "";
        if ($this->curPage != 1) {
            $prev = 0;
            $prev = $this->curPage - 1;
            $prevLinks = '<li><a href="' . $this->pageName . $this->pageVar . '=' . $prev . '"';
            $prevLinks .= " title=\"";
            $prevLinks .= $this->linkNames[$this->language]["0"];
            $prevLinks .= "\" >";
            if (!$this->useImages) {
                $prevLinks .= "&lt;"; //$this->linkNames[$this->language]["1"];
            } else {
                $prevLinks .= "<img border=\"0\" src=\"" . $this->prevImg . "\" />";
            }
            $prevLinks .= "</a></li>";
        } else {
            $prevLinks .= "<li><a href=\"$this->pageName\">&lt;</a></li>";
        }
        if (( $this->curPage ) < $totalPages) {
            $next = 0;
            $next = $this->curPage + 1;
            $nextLinks .= "<li><a href=\"";
            $nextLinks .= $this->pageName;
            $nextLinks .= $this->pageVar . "=" . $next;
            $nextLinks .= "\"";
            $nextLinks .= " title=\"";
            $nextLinks .= $this->linkNames[$this->language]["2"];
            $nextLinks .= "\" >";
            if (!$this->useImages) {
                $nextLinks .= "&gt;"; //$this->linkNames[$this->language]["3"];
            } else {
                $nextLinks .= "<img border=\"0\" src=\"" . $this->nextImg . "\" />";
            }
            $nextLinks .= "</a></li>";
        } else {
            $nextLinks .= "<li><a href=\"$this->pageName\">&gt;</a></li>";
        }
        $this->prevLinks = $prevLinks;
        $this->nextLinks = $nextLinks;
        return "<div class=\"" . $this->className . "\"><ul>" . $prevLinks . $this->pageLinks . $nextLinks . "</ul></div>" . "\n";
    }

    public function getQueryStrings() {
        $qryString = "";


        foreach ($_GET as $key => $val) {
            if ($key != $this->pageVar) {
                $qryString = ($qryString != "") ? $qryString . "&" : $qryString;
                $qryString .= $key . "=" . htmlspecialchars($val);
            }
        }
        return $qryString; //"" ;//
    }

    public function prevPage() {
        return trim($this->prevLinks);
    }

    public function nextPage() {
        return trim($this->nextLinks);
    }

}

class paging extends spa_paging {

    public function __construct() {
        parent::__construct();
    }

    /* You can even write custom code here */
}

?>